﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UserStoryTrackerDemo.Data;
using UserStoryTrackerDemo.Domain.Core.Repository;
using UserStoryTrackerDemo.Domain.Pocos;
using UserStoryTrackerDemo.Helpers;
using UserStoryTrackerDemo.Models;

namespace UserStoryTrackerDemo.Controllers
{
    public class UserStoriesController : Controller
    {
    	private IRepository<UserStory> _userStoryRepository;

    	public UserStoriesController()
    	{
    		_userStoryRepository = new Repository<UserStory>(Database.Instance.GetUserStoryData());
    	}

    	public UserStoriesController(IRepository<UserStory> userStoryRepository)
    	{
    		_userStoryRepository = userStoryRepository;
    	}

    	public ActionResult Index()
        {
        	ViewBag.Message = "User Story List";

			var userStories = _userStoryRepository.GetAll().ConvetUserStoriesToVMs();

			return View(userStories);
        }

		public ActionResult Create()
		{
			return View();
		}

		[HttpPost]
		public ActionResult Create(UserStoryVM userStoryVm)
		{
			var userStory = new UserStory()
			                	{
			                		AsA = userStoryVm.AsA,
			                		Completed = false,
			                		ICan = userStoryVm.ICan,
			                		SoThatBecause = userStoryVm.SoThatBecause,
			                	};

			_userStoryRepository.Save(userStory);

			return RedirectToAction("Index");
		}

		public ActionResult Details(int id)
		{
			UserStory userStory = _userStoryRepository.GetById(id);

			return View(userStory.ConvertUserStoryToVM());
		}

		public ActionResult Complete(int id)
		{
			UserStory userStory = _userStoryRepository.GetById(id);
			userStory.Completed = true;

			return RedirectToAction("Index");
		}
    }
}
